﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Types;

namespace Server
{
    class MainRequestManager
    {
        private static MainRequestManager instance;
        private Dictionary<string, UserRequestManager> onlineUsers;

        public static MainRequestManager Instance
        {
            get
            {
                if (instance == null)
	            {
		            instance = new MainRequestManager();
	            }
                return instance;
            }
        }

        private MainRequestManager()
        {
            onlineUsers = new Dictionary<string, UserRequestManager>();
        }

        public void NotifyAllUsers()
        {
            foreach (var user in onlineUsers)
            {
                UserType currentUser = new UserType(user.Key);
                Request request = new Request(RequestId.ServerClosed, currentUser);
                user.Value.HandleServerClosedNotifyUser(request);
            }
        }

        public bool RemoveFromOnlineUsers(string username)
        {
            try
            {
                lock (onlineUsers)
                {
                    if (onlineUsers.ContainsKey(username))
                    {
                        onlineUsers.Remove(username);
                        return true;
                    }
                    return false;
                }
            }
            catch (System.ArgumentNullException)
            {
                return false;
            }
        }

        public bool AddToOnlineUsers(UserRequestManager urm, string username)
        {
            try
            {
                lock (onlineUsers)
                {
                    onlineUsers.Add(username, urm);
                    return true;
                }
            }
            catch (System.ArgumentNullException)
            {
                return false;
            }
            catch (System.ArgumentException)
            {
                return false;
            }
        }

        public bool IsOnlineUser(UserRequestManager urm, string username)
        {
            try
            {
                lock (onlineUsers)
                {
                    if (onlineUsers.ContainsKey(username))
                    {
                        return true;
                    }
                    return false;
                }
            }
            catch (System.ArgumentNullException)
            {
                return false;
            }
        }

        public void SendQuestionToOnlineUsers()
        {
            List<string> usersList = onlineUsers.Keys.ToList();
            foreach (var user in onlineUsers)
            {
                Request request = new Request(RequestId.GetQuestionList, user.Value.GetQuestionList(user.Key));
                request.operationResult = true;
                user.Value.SendDataToUser(request);
            }
        }
    }
}
